package hk.ust.comp3111.ustnutrition.data.db;

import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Map;

import hk.ust.comp3111.ustnutrition.MiscUtils;
import hk.ust.comp3111.ustnutrition.data.*;

@DataTableName(value="records",canWrite=true,isPublic=false)
@DataTableView(value="byDate", fields={IntakeRecord.getDate.class})
public class IntakeRecord extends DataRow {
	public IntakeRecord() {
		mTime = new Date(0);
	}

	public IntakeRecord(Food food) {
		mFood = food;
		mTime = new Date();
	}

	public IntakeRecord(Food food, Date time) {
		mFood = food;
		mTime = time;
	}

	public Food getFood()
	{
		return mFood;
	}

	public Date getTime()
	{
		return mTime;
	}

	public GregorianCalendar getCalendar()
	{
		GregorianCalendar c = new GregorianCalendar();
		c.setTime(mTime);
		return c;
	}

	private Food mFood;
	private Date mTime;

	@Override
	public void loadInfo(Map<String, Object> obj) {
		super.loadInfo(obj);
		mFood = get((String)obj.get("food"), Food.class);
		mTime = new Date(MiscUtils.get(obj, "time", 0L));
	}

	@Override
	public void putInfo(Map<String, Object> obj) {
		super.putInfo(obj);
		obj.put("food", getId(mFood));
		obj.put("time", Long.toString(mTime.getTime()));
	}
	
	public static class getDate implements DataTableViewField {
		@Override
		public Object getValue(Map<String, Object> obj) {
		    return MiscUtils.getDate(new Date(MiscUtils.get(obj, "time", 0L)));
		}
	}
}


